package com.example.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mall.common.api.Result;
import com.example.mall.entity.pojo.BrandPojo;
import com.example.mall.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 品牌表(BrandPojo)表控制层
 *
 * @author cjd
 * @since 2024-04-05 03:46:35
 */
@RestController
@RequestMapping("brand")
@Api(tags = "PmsBrandController")
public class PmsBrandController {
    /**
     * 服务对象
     */
    @Resource
    private BrandService brandService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param brandPojo 查询实体
     * @return 所有数据
     */
    @GetMapping(value = "/page")
    @ApiOperation("分页获取所有品牌列表")
    @PreAuthorize("hasAuthority('brand:list')")
    public Result selectAll(Page<BrandPojo> page, BrandPojo brandPojo) {
        return Result.success(this.brandService.page(page, new QueryWrapper<>(brandPojo)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryById/{id}")
    @ApiOperation("查询品牌详情")
    public Result selectOne(@PathVariable Serializable id) {
        return Result.success(this.brandService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param brandPojo 实体对象
     * @return 新增结果
     */
    @PostMapping(value = "/insert")
    @ApiOperation("新增品牌")
    @PreAuthorize("hasAuthority('brand:create')")
    public Result insert(@RequestBody BrandPojo brandPojo) {
        return Result.success(this.brandService.save(brandPojo));
    }

    /**
     * 修改数据
     *
     * @param brandPojo 实体对象
     * @return 修改结果
     */
    @PutMapping(value = "/update")
    @ApiOperation("修改品牌")
    @PreAuthorize("hasAuthority('brand:update')")
    public Result update(@RequestBody BrandPojo brandPojo) {
        return Result.success(this.brandService.updateById(brandPojo));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("删除品牌")
    @PreAuthorize("hasAuthority('brand:delete')")
    public Result delete(@RequestParam("idList") List<Long> idList) {
        return Result.success(this.brandService.removeByIds(idList));
    }
}

